/*************************************************************
 * 
 * CopyRight(c)2007, http://www.moorwind.com/
 * email:ezhung@gmail.com
 * for free, but a email permission would be nice
 * 
 ************************************************************/
package kono.utils.collections
{
	import kono.utils.ListNode;
	
	//list iterator
	public class ListIterator implements Iiterator
	{
		//ListIterator指向的node， the node the ListIterator is pointing
		public var node:ListNode;
		
		//ListIterator引用的node, the list the ListIterator is referenced
		public var list:List;
		
		public function ListIterator(_node:ListNode = null, _list:List = null)
		{
			node = _node;
			list = _list;
		}
		
		//检查下一结点是否存在， check if next node exit
		public function hasNext():Boolean
		{
			return Boolean(node);
		}
		
		//得到结点数据并且移动到下一结点， get the data of the node and moving to the next node
		public function next():*
		{
			if(hasNext())
			{
				var data:* = node.nodeData;
				node = node.link;
				return data;
			}
			return null;
		}
		
		//移动到下一结点， move to the next node;
		public function forth():void
		{
			if(node) node = node.link;
		}
		
		public function get data():*
		{
			if(node) return node.nodeData;
			return null;
		}
		
		public function set data(item:*):void
		{
			node.nodeData = item;
		}
		
		//移动到头结点， move to the head node
		public function start():void
		{
			if(list) node = list.head;
		}
		
		//移动到尾结点， move to the tail node
		public function end():void
		{
			if(list) node = list.tail;
		}
		
		//输出当前ListIterator的描述， print the string of the ListIterator's representing
		public function toString():String
		{
			return "[ListIterator, data=" + node.nodeData + "]";
		}
	}
}